// SINGULARITY HTML5 GAME ENGINE
// -----------------------------
// Copyright(c) 2013 by Codenova.pl

define(['../Core', '../Storage', '../Mouse'], function (Core, Storage, Mouse) {
    var Ui = function(dimensions, elements) {
        this.Height = dimensions.Height;
        this.IsActive = false;
        this.Width = dimensions.Width;
        this.Elements = elements;
    };

    Ui.prototype = {
        constructor: Ui,

        Create: function(dimensions, elements) {
            "use strict";
            return new Ui(dimensions, elements);
        },

        Initialize: function(stageId) {
            // Set UI id
            this.Id = 'ui-' + stageId;

            // New storage
            this.Storage = Storage();

            // Set dimensions for auto
            (this.Width == 'auto') ? this.Width = window.innerWidth : null;
            (this.Height == 'auto') ? this.Height = window.innerHeight : null;

            // Draw UI canvas
            Core.DrawCanvas(this.Id, this.Width, this.Height);

            // UI should be on the top
            Core.SetzIndexById(this.Id, 2);

            // Get context
            this.Canvas = document.getElementById(this.Id);
            this.Context = this.Canvas.getContext('2d');

            // Draw Ui elements
            for(var _i = 0, _length = this.Elements.length - 1; _i <= _length; _i = _i + 1) {
                // If X/Y are set for 'auto', center element relative
                // to window borders
                this.Elements[_i].X = Core.CenterDimensionToWindow(this.Elements[_i].X, this.Elements[_i].Width, 'width');
                this.Elements[_i].Y = Core.CenterDimensionToWindow(this.Elements[_i].Y, this.Elements[_i].Height, 'height');

                // Create audio tag
                Core.CreateAudioTag(this.Elements[_i]);

                // Should we store this element?
                this.Storage.StoreElementForUi(this.Storage, this.Elements[_i]);

                // Initialize element
                this.Elements[_i].Initialize();

                // We assume all UI elements has prototype called Add()
                this.Elements[_i].Add();
            }

            // Attach events
            if(this.Storage.Storage.length > 0) {
                Mouse.AttachHoverEvent(this);
                Mouse.AttachClickEvent(this);
            }

            // Activate UI
            this.IsActive = true;
        }
    };

    return Ui.prototype.Create;
});